﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Caching;

namespace Arm2015.Storage
{
    public static class AppCache
    {
        private const string __D_MAKERS = "D_MAKERS";
        private const string __D_OBJECTS = "D_OBJECTS";
        private const string __D_ELEMENTS = "D_ELEMENTS";
        private const string __D_MONTHES = "D_MONTHES";

        public static Dictionary<string, bool> CacheStatus;
        public static Dictionary<string, bool> CacheRuns;

        private static Cache _cache;
        static AppCache()
        {
            CacheStatus = new Dictionary<string, bool>() { 
                {__D_MAKERS, false},
                {__D_OBJECTS, false},
                {__D_ELEMENTS, false},
                {__D_MONTHES, false}
            };
            CacheRuns = new Dictionary<string, bool>() { 
                {__D_MAKERS, true},
                {__D_OBJECTS, true},
                {__D_ELEMENTS, true},
                {__D_MONTHES, true}
            };

            if (_cache == null)
                _cache = HttpContext.Current.Cache;
        }

        public static IQueryable<Models.Fact.Month> D_MONTHES
        {
            get
            {
                if (CacheStatus[__D_MONTHES] == false)
                {
                    if (CacheRuns[__D_MONTHES] == true)
                    {
                        Task.Run(() =>
                        {
                            _cache[__D_MONTHES] = Fact.D_MONTHES.ToList();
                            CacheRuns[__D_MONTHES] = false;
                            CacheStatus[__D_MONTHES] = true;
                        });
                    }
                    return Fact.D_MONTHES;
                }
                else
                {
                    return ((List<Models.Fact.Month>)_cache[__D_MONTHES]).AsQueryable();
                }
            }
        }

        public static IQueryable<Models.Fact.Maker> D_MAKERS 
        { 
            get
            {
                if (CacheStatus[__D_MAKERS]==false)
                {
                    if (CacheRuns[__D_MAKERS] == true)
                    {
                        Task.Run(() =>
                        {
                            _cache[__D_MAKERS] = Fact.D_MAKERS.ToList();
                            CacheRuns[__D_MAKERS] = false;
                            CacheStatus[__D_MAKERS] = true;
                        });
                    }
                    return Fact.D_MAKERS;
                }
                else
                {
                    return ((List<Models.Fact.Maker>)_cache[__D_MAKERS]).AsQueryable();
                }
            }
        }

        public static IQueryable<Models.Fact.Object> D_OBJECTS
        {
            get
            {
                if (CacheStatus[__D_OBJECTS] == false)
                {
                    if (CacheRuns[__D_OBJECTS] == true)
                    {
                        Task.Run(() =>
                        {
                            _cache[__D_OBJECTS] = Fact.D_OBJECTS.ToList();
                            CacheRuns[__D_OBJECTS] = false;
                            CacheStatus[__D_OBJECTS] = true;
                        });
                    }
                    return Fact.D_OBJECTS;
                }
                else
                {
                    return ((List<Models.Fact.Object>)_cache[__D_OBJECTS]).AsQueryable();
                }
            }
        }

        public static IQueryable<Models.Fact.ObjectElement> D_ELEMENTS
        {
            get
            {
                if (CacheStatus[__D_ELEMENTS] == false)
                {
                    if (CacheRuns[__D_ELEMENTS] == true)
                    {
                        Task.Run(() =>
                        {
                            _cache[__D_ELEMENTS] = Fact.D_ELEMENTS.ToList();
                            CacheRuns[__D_ELEMENTS] = false;
                            CacheStatus[__D_ELEMENTS] = true;
                        });
                    }
                    return Fact.D_ELEMENTS;
                }
                else
                {
                    return ((List<Models.Fact.ObjectElement>)_cache[__D_ELEMENTS]).AsQueryable();
                }
            }
        }
    }
}
